package com.example.iread.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;

import com.example.iread.R;
import com.example.iread.activity.BookHistoryActivity;
import com.example.iread.activity.LoginActivity;
import com.example.iread.activity.ReadMeActivity;
import com.example.iread.activity.UserInfoActivity;
import com.example.iread.application.MyApplication;
import com.example.iread.dao.UserDao;
import com.example.iread.util.WindowUtil;
import com.google.android.material.switchmaterial.SwitchMaterial;

public class PersonFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person, container, false);

        // 获取数据库
        UserDao userDao = MyApplication.getInstance().getDb().userDao();

        TextView tv_nick_name = view.findViewById(R.id.tv_nick_name);
        tv_nick_name.setText(userDao.getOne().nickname);

        // 跳转个人信息
        View btn_personal_information = view.findViewById(R.id.btn_personal_information);
        btn_personal_information.setOnClickListener(v -> {
            Intent intent = new Intent(view.getContext(), UserInfoActivity.class);
            startActivity(intent);
        });

        // 跳转阅读历史
        View btn_reading_history = view.findViewById(R.id.btn_reading_history);
        btn_reading_history.setOnClickListener(v -> {
            Intent intent = new Intent(view.getContext(), BookHistoryActivity.class);
            startActivity(intent);
        });

        // 跳转ReadMe
        View btn_readme = view.findViewById(R.id.btn_readme);
        btn_readme.setOnClickListener(v -> {
            Intent intent = new Intent(view.getContext(), ReadMeActivity.class);
            startActivity(intent);
        });

        // 深色模式切换
        SwitchMaterial sw_toggle_night_mode = view.findViewById(R.id.sw_toggle_night_mode);
        SharedPreferences setting = view.getContext().getSharedPreferences("setting", Context.MODE_PRIVATE);
        boolean nightMode = setting.getBoolean("night_mode", false);
        sw_toggle_night_mode.setChecked(nightMode);
        sw_toggle_night_mode.setOnCheckedChangeListener((v, isCheck) -> {
//            Log.i("SoRiya", "onCreateView: " + isCheck);
            AppCompatDelegate.setDefaultNightMode(isCheck ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
            SharedPreferences.Editor edit = setting.edit();
            edit.putBoolean("night_mode", isCheck);
            edit.apply();
        });

        Button btn_sign_out = view.findViewById(R.id.btn_sign_out);
        btn_sign_out.setOnClickListener(v -> {
            SharedPreferences userInfo = view.getContext().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = userInfo.edit();
            edit.remove("token");
            edit.apply();
            Intent intent = new Intent(view.getContext(), LoginActivity.class);
            startActivity(intent);
        });

        return view;
    }
}